package lib.quasar.widget.pager;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import lib.quasar.widget.R;

/**
 * description: 滑动开关
 * created by kalu on 2017/3/18 18:53
 */
public class SwitchViewPager extends ViewPager {

    private boolean enableScroll = false;

    public SwitchViewPager(Context context) {
        this(context, null);
    }

    public SwitchViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SwitchViewPager);

        try {
            enableScroll = array.getBoolean(R.styleable.SwitchViewPager_svp_switch_pager, false);
        } catch (Exception e) {
            //  LogUtil.e("SwitchViewPager", e.getMessage(), e);
        } finally {
            array.recycle();
        }
    }

    public void setEnableScroll(boolean enableScroll) {
        this.enableScroll = enableScroll;
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (enableScroll) {
            return super.onTouchEvent(arg0);
        } else {
            return false;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (enableScroll) {
            return super.onInterceptTouchEvent(arg0);
        } else {
            return false;
        }
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }
}